package com.cold.legendary.bprocess.image.manager;


import com.cold.legendary.bprocess.context.ins.BPointIns;
import com.cold.legendary.bprocess.context.ins.BProcessIns;
import com.cold.legendary.bprocess.context.manager.BProcessManager;
import com.cold.legendary.bprocess.image.model.BArrowInsGraphicInfo;
import com.cold.legendary.bprocess.image.model.BPointGraphicInfo;
import com.cold.legendary.bprocess.image.model.GraphicInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * @author cold
 * @since 2023/6/12
 */
public class BProcessImageManager {

    /**
     * 所有图形信息
     */
    private static ConcurrentHashMap<String, List<GraphicInfo>> allProcessGraphicInfos = new ConcurrentHashMap<>();

    /**
     * 获取流程图形信息
     *
     * @param processId
     * @return
     */
    public static List<GraphicInfo> getProcessGraphicInfo(String processId) {
        return allProcessGraphicInfos.get(processId);
    }

    /**
     * 加载所有图形信息
     */
    public static void loadGraphicInfos() {
        ConcurrentHashMap<String, BProcessIns> bprocessInsMap = BProcessManager.getBprocessInsMap();
        for (Map.Entry<String, BProcessIns> bProcessInsEntry : bprocessInsMap.entrySet()) {
            String processId = bProcessInsEntry.getKey();
            BProcessIns bProcessIns = bProcessInsEntry.getValue();

            int defaultX = 50;
            BPointIns startBPointIns = bProcessIns.getStartBPointIns();
            List<GraphicInfo> graphicInfos = new ArrayList<>();
            calculateGraphicInfo(defaultX, startBPointIns, graphicInfos);
            allProcessGraphicInfos.put(processId, graphicInfos);
        }
    }

    /**
     * 递归调用计算x，y轴线数据
     *
     * @param x
     * @param bPointIns
     * @param graphicInfos
     */
    private static void calculateGraphicInfo(int x, BPointIns bPointIns, List<GraphicInfo> graphicInfos) {
        if (null == bPointIns) {
            return;
        }
        BPointGraphicInfo pointGraphicInfo = new BPointGraphicInfo();
        pointGraphicInfo.setShapeId(bPointIns.getPointId());
        pointGraphicInfo.setShapeName(bPointIns.getPointName());
        pointGraphicInfo.setSrcX(x);
        graphicInfos.add(pointGraphicInfo);
        if (null == bPointIns.getNextBArrowIns()) {
            return;
        }

        int arrowX = x + pointGraphicInfo.getWidth();
        int arrowY = pointGraphicInfo.getSrcY() + pointGraphicInfo.getHeight()/2;
        BArrowInsGraphicInfo arrowGraphicInfo = new BArrowInsGraphicInfo();
        arrowGraphicInfo.setSrcX(arrowX);
        arrowGraphicInfo.setSrcY(arrowY);
        arrowGraphicInfo.setTargetX(arrowX + arrowGraphicInfo.getWidth());
        arrowGraphicInfo.setTargetY(arrowY);
        graphicInfos.add(arrowGraphicInfo);

        int newBPointX = arrowX + arrowGraphicInfo.getWidth();
        //递归调用
        calculateGraphicInfo(newBPointX, bPointIns.getNextBArrowIns().getNextBPointIns(), graphicInfos);
    }

}
